﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Forms.Control.Employee;
using ZhimeiSoft.CharmManager.DOL;
namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class EmployeeInfoBrows : BaseUserControl
    {
        //店信息
        private SortedList<int, string> sortSalonInfo = new SortedList<int,string>(); 

        public EmployeeInfoBrows()
        {
            InitializeComponent();
            cbxEmployeeState.SelectedItem = cbxF0E;
            string state = DBEntityEmployee.ENUM_ESTATE.ChineseStrToEnumStr(cbxEmployeeState.SelectedItem.ToString());
            initData(state);
            initSalonInfo();
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {

        }

        private void btnAddEmployee_Click(object sender, EventArgs e)
        {
            frmEmployeeAdd frmadd = new frmEmployeeAdd();
            frmadd.employeeInfo = null;
            if (frmadd.ShowDialog() == DialogResult.OK)
            {
                //刷新数据集
                string state = DBEntityEmployee.ENUM_ESTATE.ChineseStrToEnumStr(cbxEmployeeState.SelectedItem.ToString());
                initData(state);
            }
        }

        private void btnEditEmployee_Click(object sender, EventArgs e)
        {
            if (cardviewEmployee.FocusedRowHandle < 0) {
                MsgBox.MsgInfo("请选择正确的员工记录。");
                return;
            }
            DataRow drEmployee = cardviewEmployee.GetDataRow(cardviewEmployee.FocusedRowHandle);
            DBEntityEmployee employeeInfo = new DBEntityEmployee(drEmployee);
            frmEmployeeAdd frmadd = new frmEmployeeAdd();
            frmadd.employeeInfo = employeeInfo;
            if (frmadd.ShowDialog() == DialogResult.OK)
            {
                //刷新数据集
                string state = DBEntityEmployee.ENUM_ESTATE.ChineseStrToEnumStr(cbxEmployeeState.SelectedItem.ToString());
                initData(state);
            }    
        }

        private void initData(string employeeState)
        {
            DataSet dsEmployee;
            string error = string.Empty;

            if (!BussinessProxyManager.Instance.QueryEmployeeState(employeeState, out dsEmployee, out error))
            {
                MsgBox.MsgInfo("获取员工数据出现错误：" + error);
                return;
            }
            gdEmployeeInfo.DataSource = dsEmployee.Tables[0];
        }

        private void initSalonInfo()
        {
            DataSet dsSalon = new DataSet();
            string error = string.Empty;
            if(!BussinessProxyManager.Instance.QuerySalonInfo(out dsSalon, out error)){
                MsgBox.MsgInfo("初始化店信息错误：" + error);
                return;
            }
            foreach (DataRow dr in dsSalon.Tables[0].Rows)
            {
                sortSalonInfo.Add(Convert.ToInt32(dr[DBEntitySalonInfo.FieldName.SALONID]),
                    dr[DBEntitySalonInfo.FieldName.SNAME].ToString());
            }
        }

        private void cardviewEmployee_CustomDrawCardFieldCaption(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {

        }

        private void cardviewEmployee_CustomDrawCardCaption(object sender, DevExpress.XtraGrid.Views.Card.CardCaptionCustomDrawEventArgs e)
        {
           
        }

        private void cardviewEmployee_CustomDrawCardFieldValue(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == DBEntityEmployee.FieldName.ESTATE)
            {
                Rectangle r = e.Bounds;
                string sText = DBEntityEmployee.ENUM_ESTATE.EnumStrToChineseStr(e.CellValue.ToString());
                e.Style.DrawString(e.Graphics, sText, r);
                e.Handled = true;
            }
            else if (e.Column.FieldName == DBEntityEmployee.FieldName.SALONID)
            {
                Rectangle r = e.Bounds;
                int idx = sortSalonInfo.IndexOfKey(Convert.ToInt32(e.CellValue.ToString()));
                if (idx == -1)
                {
                    string sText = sortSalonInfo.Values[idx];
                    e.Style.DrawString(e.Graphics, sText, r);
                    e.Handled = true;
                }
            }
        }

        
    }
}
